package cliente.gerenciadorcliente.iu;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import cliente.gerenciadorcliente.rn.RNSalaPrincipal;
import cliente.so.SOGeradorPopup;
import cliente.so.SOPopup;

import comum.so.exception.SalaCheiaException;
import comum.so.exception.SalaNaoExisteException;
import comum.so.exception.SenhaIncorretaException;

public class IUEntradaSenhaSalaJogo extends JFrame {

	private static final long serialVersionUID = 1527394998733903551L;

	public IUEntradaSenhaSalaJogo(final Long idSalaJogo,
			final RNSalaPrincipal salaPrincipal, final JFrame frame) {
		setTitle("Entre com a senha da sala de jogo");
		frame.setEnabled(false);
		JPanel panelSenha = new JPanel();
		panelSenha.add(new JLabel("Senha: "));
		final JPasswordField passwordFieldSenha = new JPasswordField();
		passwordFieldSenha.setColumns(10);
		panelSenha.add(passwordFieldSenha);
		getContentPane().add(panelSenha, BorderLayout.NORTH);
		JPanel panelBotoes = new JPanel();
		JButton btnConfirmar = new JButton("Confirmar");
		btnConfirmar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					salaPrincipal.entrarSalaJogo(idSalaJogo, new String(
							passwordFieldSenha.getPassword()));
				} catch (RemoteException e) {
					e.printStackTrace();
					SOGeradorPopup.getPopup(SOPopup.CONEXAO_PERDIDA);
				} catch (SalaCheiaException e) {
					e.printStackTrace();
					SOGeradorPopup.getPopup(SOPopup.JOGO_CHEIO);
				} catch (SalaNaoExisteException e) {
					e.printStackTrace();
					SOGeradorPopup.getPopup(SOPopup.JOGO_NAO_ENCONTRADO);
				} catch (SenhaIncorretaException e) {
					e.printStackTrace();
					SOGeradorPopup.getPopup(SOPopup.SENHA_INCORRETA);
				}
				frame.setEnabled(true);
				dispose();
			}
		});
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.setEnabled(true);
				dispose();
			}
		});
		panelBotoes.add(btnConfirmar);
		panelBotoes.add(btnCancelar);
		getContentPane().add(panelBotoes, BorderLayout.SOUTH);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				frame.setEnabled(true);
			}
		});
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = getSize();
		setLocation(screenSize.width / 2 - frameSize.width / 2,
				screenSize.height / 2 - frameSize.height / 2);
		setVisible(true);
		setResizable(false);
		pack();
	}

}
